package sysu.el.array.ints.algors;

import sysu.el.array.algors.Sig;

/*
 * The a1, a2 are sorted.
 */

public class CommonElemFinder {
	static public int comElemNum;
	public static int[] cal(int n1, int a1[], int n2, int a2[]){
		assert(InOrder.asc(n1, a1));
		assert(InOrder.asc(n2, a2));
		
		int num = Math.min(n1, n2);
//		int num = Math.max(n1, n2);
		comElemNum = 0;
		int[] comElems = new int[num];
		int j=0;
		System.out.println(Sig.mkString(n1, a1, "\t"));
		System.out.println(Sig.mkString(n2, a2, "\t"));
//		System.out.println();
		for (int i=0; i<n1; i++){
			while ((j < n2) && a1[i]>a2[j])
				j ++;
			if (j == n2) break;
			if (a1[i] == a2[j])
				comElems[comElemNum ++] = a1[i];
		}
		return comElems;
	}
	
	public static void main(String argv[]){
		int a[] = {1, 2, 3};
		int b[] = {1, 3};
		int k[] = CommonElemFinder.cal(a.length, a, b.length, b);
		System.out.println("Num:\t" + CommonElemFinder.comElemNum);
		for (int e: k){
			System.out.print(e + "\t");
		}
	}
}
